package com.cqust.icc.sqlite;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.cqust.icc.sqlite.db.User;

import java.util.ArrayList;

public final class MyAdapter extends BaseAdapter {

    private final ArrayList<User> users;

    private final Context context;
    private MyClick myClick;

    public void setMyClick(MyClick myClick) {
        this.myClick = myClick;
    }


    public MyAdapter(ArrayList<User> users, Context context) {
        this.users = users;
        this.context = context;
    }

    @Override
    public int getCount() {
        return users.size();
    }

    @Override
    public User getItem(int i) {
        return users.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if (view == null) {
            viewHolder = new ViewHolder();
            view = LayoutInflater.from(context).inflate(R.layout.item_main_list, null);
            viewHolder.tv1 = view.findViewById(R.id.item_tv1);
            viewHolder.tv2 = view.findViewById(R.id.item_tv2);
            viewHolder.tv3 = view.findViewById(R.id.item_tv3);
            viewHolder.tv4 = view.findViewById(R.id.item_tv4);
            viewHolder.btn_delete = view.findViewById(R.id.btn_delete);
            viewHolder.btn_add = view.findViewById(R.id.btn_add);
            viewHolder.btn_sub = view.findViewById(R.id.btn_sub);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }
        User user = users.get(i);
        if (myClick != null) {
            viewHolder.btn_delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    myClick.deleteClick(user);
                }
            });
            viewHolder.btn_add.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    myClick.addClick(user);
                }
            });
            viewHolder.btn_sub.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    myClick.subClick(user);
                }
            });
        }

        if (user != null) {
            viewHolder.tv1.setText(user.getId());
            viewHolder.tv2.setText(user.getName());
            viewHolder.tv3.setText(user.getPassword());
            viewHolder.tv4.setText(String.valueOf(user.getAge()));
        }
        return view;
    }

    static class ViewHolder {
        TextView tv1;
        TextView tv2;
        TextView tv3;
        TextView tv4;
        TextView btn_delete;
        TextView btn_add;
        TextView btn_sub;
    }
}

interface MyClick {
    /*
     * 删除 按钮 回调
     * */
    void deleteClick(User user);

    /*
     * UP 按钮 回调
     * */
    void addClick(User user);

    /*
     * Down 按钮 回调
     * */
    void subClick(User user);

}
